package com.thd.springboot.project.codegenerator.controller;
import com.thd.springboot.framework.web.controller.BasicController;
import com.thd.springboot.framework.web.utils.WebUtils;
import com.thd.springboot.project.codegenerator.dto.GeneratorDto;
import com.thd.springboot.project.codegenerator.dto.GeneratResultDto;
import com.thd.springboot.project.codegenerator.entity.GeneratorTemplateEntity;
import com.thd.springboot.project.codegenerator.service.CodeGenService;
import com.thd.springboot.project.codegenerator.service.GeneratorTemplateService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.thd.springboot.framework.model.Message;

import javax.servlet.http.HttpServletResponse;
import java.util.*;


/**
 * controller.GeneratorTemplateController
 **/
@RestController
@RequestMapping("/generatorTemplate")
public class  GeneratorTemplateController extends BasicController {

	@Autowired
	private GeneratorTemplateService generatorTemplateService;
    @Autowired
    private CodeGenService codeGenService;
	@ResponseBody
    @PostMapping("/addGeneratorTemplate")
    // url : http://127.0.0.1:8899/thd/cg/addGeneratorTemplate
    public Message addGeneratorTemplate(@RequestBody GeneratorTemplateEntity entity){
        entity.setId(UUID.randomUUID().toString());
        this.generatorTemplateService.create(entity);
        return Message.success("SUCCESS");
    }

    @ResponseBody
    @PostMapping("/updateGeneratorTemplate")
    // url : http://127.0.0.1:8899/thd/cg/updateGeneratorTemplate
    public Message updateGeneratorTemplate(@RequestBody GeneratorTemplateEntity entity){
        int updateCount = this.generatorTemplateService.modify(entity);
        if(updateCount!=1){
            throw new RuntimeException(" Update Failed !");
        }
        return Message.success("SUCCESS");
    }

    @ResponseBody
    @DeleteMapping("/physicsDeleteGeneratorTemplate/{id}")
    // url : http://127.0.0.1:8899/thd/cg/physicsDeleteGeneratorTemplate/15
    public Message physicsDeleteGeneratorTemplate(@PathVariable String id){
        this.generatorTemplateService.deletePhysicsById(id);
        return Message.success("SUCCESS");
    }

    @ResponseBody
    @DeleteMapping("/logicDeleteGeneratorTemplate/{id}")
    // url : http://127.0.0.1:8899/thd/cg/logicDeleteGeneratorTemplate/15
    public Message logicDeleteGeneratorTemplate(@PathVariable String id){
        this.generatorTemplateService.deleteLogicById(id);
        return Message.success("SUCCESS");
    }


    @ResponseBody
    @DeleteMapping("/deleteLogicByGeneratorTemplateIds")
    // url : http://127.0.0.1:8899/thd/cg/deleteLogicByGeneratorTemplateIds
    public Message deleteLogicByGeneratorTemplateIds(@RequestBody List<String> ids){
        List<Object> idObjList = new ArrayList<Object>();
        ids.forEach(id -> idObjList.add(id));
        this.generatorTemplateService.deleteLogicByIds(idObjList);
        return Message.success("SUCCESS");
    }

    @ResponseBody
    @RequestMapping("/queryGeneratorTemplateById/{id}")
    // url : http://127.0.0.1:8899/thd/cg/queryGeneratorTemplateById/2
    public Message queryGeneratorTemplate(@PathVariable String id){
        GeneratorTemplateEntity entity = this.generatorTemplateService.queryById(id);
        return Message.success(entity);
    }

    @ResponseBody
    @RequestMapping("/queryGeneratorTemplateEqByPage")
    // url : http://127.0.0.1:8899/thd/cg/queryGeneratorTemplateEqByPage
    public Message queryGeneratorTemplateEqByPage(@RequestBody GeneratorTemplateEntity entity){
        PageInfo pi = this.generatorTemplateService.queryListEqByPage(entity);
        return Message.success(pi);
    }

    @ResponseBody
    @RequestMapping("/queryGeneratorTemplateLikeByPage")
    // url : http://127.0.0.1:8899/thd/cg/queryGeneratorTemplateLikeByPage
    public Message queryGeneratorTemplateLikeByPage(@RequestBody GeneratorTemplateEntity entity){
        PageInfo pi = this.generatorTemplateService.queryListLikeByPage(entity);
        return Message.success(pi);
    }

//    @ResponseBody
//    @RequestMapping("/insertGeneratorTemplateBatch")
//    // url : http://127.0.0.1:8899/thd/cg/insertGeneratorTemplateBatch
//    public Message insertGeneratorTemplateBatch(){
//
//        List<GeneratorTemplateEntity> l = new ArrayList<GeneratorTemplateEntity>();
//        for(int i = 0 , j = 10 ; i < j ; i++){
//            GeneratorTemplateEntity entity = new GeneratorTemplateEntity();
//            entity.setId("id_" + i );
//            entity.setUserAge(i);
//            entity.setUserBirthday(new Date());
//            entity.setUserName("devil13th_" + i);
//            l.add(entity);
//
//        }
//        this.generatorTemplateService.insertBatch(l);
//        return Message.success("Success");
//    }


    @PostMapping("/generateToZip")
    @ResponseBody
    // url : http://127.0.0.1:2349/generatorTemplate/generateToZip
    public Message generateToZip(@RequestBody GeneratorDto generatorDto, HttpServletResponse response) throws Exception{
        String zipFilePath = codeGenService.createCodeToZip(generatorDto.getIds(),generatorDto.getTableNames());
        WebUtils.download(response,zipFilePath,"code.zip");
        return null;
    }

    @PostMapping("/generateToData")
    @ResponseBody
    // url : http://127.0.0.1:2349/generatorTemplate/generateToData
    public Message generateToData(@RequestBody GeneratorDto generatorDto, HttpServletResponse response) throws Exception{
        Map<String,List<GeneratResultDto>> m = codeGenService.createCodeToData(generatorDto.getIds(),generatorDto.getTableNames());
        return Message.success(m);
    }


    @GetMapping("/generateStr/{tableName}/{id}")
    @ResponseBody
    // url : http://127.0.0.1:2349/generatorTemplate/generateStr/generator_template/1
    public Message generateStr(@PathVariable String tableName,@PathVariable String id) throws Exception{
        String content = codeGenService.createCode(id,tableName);
        return Message.success(content);
    }




}

